﻿//input characters limit plugin
(function($) {
    $.fn.extend({
        limitInput: function(options) {
            //options
            var defaults = {
                limit: 500,
                trim: false,
                msgElem: ""
            };
            var options = $.extend(defaults, options);
            //function body
            return this.each(function() {
                var obj = $(this);
                //bind keyup function to all input fields
                obj.keyup(function(e) {
                    //get field input
                    var val = obj.val();
                    //trim if asked
                    if (options.trim)
                        val = $.trim(val);
                    //truncate if over limit
                    if (val.length > options.limit)
                        obj.val(val.substring(0, options.limit));
                    //show char left msg
                    if (options.msgElem != "")
                        $("#" + options.msgElem).html((options.limit - val.length) + " characters left");
                });
            });
        }
    });
})(jQuery);